import requests
from lxml import etree
from multiprocessing.dummy import Pool


headers={
    'User-agent' :'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36 Edg/99.0.1150.55',
}

url='https://www.pearvideo.com/category_5'
pageText=requests.get(url=url,headers=headers).text
tree=etree.HTML(pageText)
lilist=tree.xpath('//*[@id="listvideoListUl"]/li')
urls=[]
print(lilist)
for li in lilist:
    href=li.xpath('./div/a/@href')[0]
    videoId=href.split('_')[1]
    headers = {
        'User-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36 Edg/99.0.1150.55',
        'Referer': 'https://www.pearvideo.com/' + href,
        'Cookie': '__secdyid=fd31b396ff030a86eec1206df1128bd10144b86cec41d6dd021648125595; PEAR_UUID=6f0bfe95-577b-4a3a-b38d-ce7899053357; _uab_collina=164812559621176860145185; UM_distinctid=17fbbf0d615412-0f09f404c29b36-5617185b-144000-17fbbf0d6161063; p_h5_u=108957EB-8D3E-4373-BB8C-23A341FB99C5; JSESSIONID=226357401058E3C72BD49520E4ABEFC8; CNZZDATA1260553744=1233658928-1648115834-https%253A%252F%252Fcn.bing.com%252F%7C1648540035; Hm_lvt_9707bc8d5f6bba210e7218b8496f076a=1648125599,1648540651; Hm_lpvt_9707bc8d5f6bba210e7218b8496f076a=1648542124; acw_tc=2f624a0216485425995855947e78e7b3a1ff38052ccec9682500853d7fefe6; SERVERID=bacac21aafa9027952fdc46518c0c74f|1648542599|1648540642'
    }

    videoHref='https://www.pearvideo.com/videoStatus.jsp?contId='+videoId
    videoUrl=requests.get(url=videoHref,headers=headers).json()
    video=videoUrl["videoInfo"]["videos"]["srcUrl"]
    video=video.replace(video.split('/')[-1].split('-')[0],'cont-%s' %videoId)
    name=video.split('/')[-1]
    dic={
        'name':name,
        'url':video
    }
    urls.append(dic)
    print(urls)


def getVideoData(dic):
    url=dic['url']
    videoData=requests.get(url=url,headers=headers).content
    with open(dic['name'],'wb') as fp:
        fp.write(videoData)
        print(dic['name'],'下载成功！！！')
    
pool=Pool(4)
pool.map(getVideoData,urls)